<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>查看头像测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .container {
            border: 1px solid #ddd;
            padding: 20px;
            border-radius: 5px;
            margin-top: 20px;
        }
        .avatar-container {
            margin-top: 20px;
            text-align: center;
        }
        #avatar {
            max-width: 200px;
            max-height: 200px;
            border-radius: 50%;
            display: none;
        }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background-color: #45a049;
        }
        #result {
            margin-top: 20px;
            padding: 10px;
            border-radius: 4px;
        }
        .success {
            background-color: #dff0d8;
            color: #3c763d;
        }
        .error {
            background-color: #f2dede;
            color: #a94442;
        }
        .loading {
            text-align: center;
            margin: 20px 0;
            display: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>查看头像测试</h2>
        <div class="form-group">
            <button onclick="getAvatar()">获取头像</button>
        </div>
        <div class="loading" id="loading">加载中...</div>
        <div class="avatar-container">
            <img id="avatar" alt="用户头像">
        </div>
        <div id="result"></div>
    </div>

    <script>
        function getAvatar() {
            const resultDiv = document.getElementById('result');
            const avatarImg = document.getElementById('avatar');
            const loadingDiv = document.getElementById('loading');
            
            // 显示加载中
            loadingDiv.style.display = 'block';
            avatarImg.style.display = 'none';
            resultDiv.textContent = '';

            // 使用固定的token
            const token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJzY2hvb2xib29rc2hvcCIsImlhdCI6MTc0NDc4NjQxOSwiZXhwIjoxNzQ0ODcyODE5LCJ1c2VySWQiOiIxIiwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOiJBRE1JTiJ9.3lyEcMQs2Q0IPG4Oyt7zaiTVUVPN1lqR1g1Nt7Z2blw';

            fetch('http://localhost:8080/api/profile/downloadAvatar', {
                method: 'GET',
                headers: {
                    'Authorization': 'Bearer ' + token
                }
            })
            .then(response => response.json())
            .then(result => {
                // 隐藏加载中
                loadingDiv.style.display = 'none';

                if (result.code === 200) {
                    // 创建Blob对象
                    const avatarData = result.data.avatar;
                    const avatarType = result.data.avatarType;
                    
                    // 将Base64字符串转换为Blob
                    const byteCharacters = atob(avatarData);
                    const byteNumbers = new Array(byteCharacters.length);
                    for (let i = 0; i < byteCharacters.length; i++) {
                        byteNumbers[i] = byteCharacters.charCodeAt(i);
                    }
                    const byteArray = new Uint8Array(byteNumbers);
                    const blob = new Blob([byteArray], { type: avatarType });
                    
                    // 创建图片URL
                    const imageUrl = URL.createObjectURL(blob);
                    
                    // 显示图片
                    avatarImg.src = imageUrl;
                    avatarImg.style.display = 'block';
                    
                    resultDiv.className = 'success';
                    resultDiv.textContent = '获取头像成功';
                } else {
                    resultDiv.className = 'error';
                    resultDiv.textContent = '获取头像失败：' + result.message;
                }
            })
            .catch(error => {
                loadingDiv.style.display = 'none';
                resultDiv.className = 'error';
                resultDiv.textContent = '请求失败：' + error.message;
            });
        }
    </script>
</body>
</html> 